package sicnu.cs.ich.log.mq;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Service;
import sicnu.cs.ich.common.services.impl.RedisService;
import sicnu.cs.ich.rocketmq.rocketmq.AbstractRocketConsumer;

/**
 * @author CaiKe
 * @date 2021/12/9 16:17
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RedisConsumer extends AbstractRocketConsumer {
    private final RedisService redisService;

    @Override
    public void init() {
        super.necessary("redis", "*", "Redis删除缓存消费者");
        handleMessage();
    }

    public ConsumeConcurrentlyStatus del(MessageExt message) {
        // 重复次数2次
        final String key = (String) JSON.parse(message.getBody());
        if (redisService.removeValue(key)) {
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } else {
            log.error("REDIS KEY [" + key + "]删除失败, 稍后重试");
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    }

}